package io.gitee.yxsnake.framework.core.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.gitee.yxsnake.framework.core.domain.Result;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.json.JSONUtil;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author snake
 * @description
 * @since 2024/11/19 0:22
 */
@Slf4j
public class ResultUtils<T> {

    public static <T> void checkResult(Result<T> result){
        log.info("返回结果：" + JSONUtil.toJsonStr(result));
        isNull(result);
        isSuccess(result);
    }

    public static <T> T getResultData(Result<T> result){
        checkResult(result);
        return result.getData();
    }


    public static <T> T getResultData(Result<T> result, T errorResult){
        try{
            return getResultData(result);
        }catch (Exception e){
            return errorResult;
        }
    }

    public static <T,R> R getResultDataWithTrans(Result<T> result, Function<T,R> transFunction){
        checkResult(result);
        return transFunction.apply(result.getData());
    }

    public static <T,R> R getResultDataWithTrans(Result<T> result, Function<T,R> transFunction, R errorResult){
        try{
            return getResultDataWithTrans(result, transFunction);
        }catch (Exception e){
            return errorResult;
        }
    }

    public static <T,R> List<R> getResultDataWithTransList(Result<List<T>> result, Function<T,R> transFunction){
        checkResult(result);
        return transList(result.getData(), transFunction);
    }

    public static <T,R> List<R> getResultDataWithTransList(Result<List<T>> result, Function<T,R> transFunction, List<R> errorResult){
        try{
            return getResultDataWithTransList(result, transFunction);
        }catch (Exception e){
            return errorResult;
        }
    }

    public static <T,R> Page<R> getResultDataWithTransPage(Result<Page<T>> result, Function<T,R> transFunction){
        checkResult(result);
        return transPage(result.getData(), transFunction);
    }


    public static <T,R> Page<R> getResultDataWithTransPage(Result<Page<T>> result, Function<T,R> transFunction, Page<R> errorResult){
        try{
            return getResultDataWithTransPage(result, transFunction);
        }catch (Exception e){
            return errorResult;
        }
    }

////////////////// private method //////////////////
    /**
     * Page对象转换
     * @param page            需要被转换的page对象
     * @param transFunction   转换函数
     * @param <T>             源类型
     * @param <R>             转换后类型
     * @return                转换后的page对象
     */
    private static <T,R> Page<R> transPage(Page<T> page, Function<T,R> transFunction){
        Page<R> resultPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        resultPage.setRecords(transList(page.getRecords(), transFunction));
        return resultPage;
    }

    /**
     * Page对象转换
     * @param coll            需要被转换的page对象
     * @param transFunction   转换函数
     * @param <T>             源类型
     * @param <R>             转换后类型
     * @return                转换后的page对象
     */
    private static <T,R> List<R> transList(Collection<T> coll, Function<T,R> transFunction){
        return coll.stream().map(transFunction).collect(Collectors.toList());
    }

    /**
     * 判斷返回值是否為null
     *
     * @param result 返回对象
     */
    private static void isNull(Result result){
        BizAssert.isTrue("result is null", Objects.isNull(result));
    }

    /**
     * 判断请求是否成功
     *
     * @param result 返回对象
     */
    private static void isSuccess(Result result){
        BizAssert.isTrue(result.getMsg(),!result.getCode().equals(Result.SUCCESS));
    }

}

